package com.info33.platform.business.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.business.app.dao.AppRoleDao;
import com.info33.platform.business.app.dao.AppRoleUserDao;
import com.info33.platform.business.app.entity.AppRole;
import com.info33.platform.business.app.entity.AppRoleUser;
import com.info33.platform.business.app.service.AppRoleService;
import com.info33.platform.business.app.vo.AppRoleVO;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin
 */
@Service
public class AppRoleServiceImpl extends ServiceImpl<AppRoleDao, AppRole> implements AppRoleService {

    @Autowired
    private AppRoleDao appRoleDao;
    @Autowired
    private AppRoleUserDao appRoleUserDao;

    @Override
    public Result<Object> pcRemove(Long id) {
        Integer num = appRoleUserDao.selectCount(new QueryWrapper<AppRoleUser>().eq("role_id", id));
        if (num > 0) {
            return Result.error("此角色有已绑定 " + num + " 个用户，不可删除");
        }
        return appRoleDao.deleteById(id) == 1 ? Result.ok() : Result.error();
    }

    @Override
    public List<AppRoleVO> appRoles(SysUserVO appCurrentUser) {
        return appRoleDao.appRoles(appCurrentUser);
    }

}